<?php
require_once('../../config.php');
require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_connection', 'yui_json'));
global $CFG, $USER, $COURSE;
require_once("$CFG->dirroot/blocks/moodlelibreserve/lib.php");
$rec_per_pg = 8;
$offset = optional_param('offset', 0, PARAM_INT);
$req_filter = optional_param('req_filters', '', PARAM_RAW);
$proc_filter = optional_param('proc_filters', '', PARAM_RAW);
$context = get_context_instance(CONTEXT_SYSTEM);
require_js($CFG->wwwroot.'/blocks/moodlelibreserve/libres.js');
$cat_id = array('', 'Call No.', 'ISBN No.', 'Instructor\'s copy', 'Call No.', 'ISBN No.', 'Instructor\'s copy', 'Call No.', 'ISBN No.', 'Instructor\'s copy');
$cat_tb = array('', 'call_num', 'isbn_num', 'instr_copy', 'call_num', 'isbn_num', 'instr_copy', 'call_num', 'isbn_num', 'instr_copy');
$months = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/blocks/moodlelibreserve/style.css" />';
if($req_filter != '' || $proc_filter != ''){
	$cond = 'WHERE ';
}

$args = array();
foreach(split('&', urldecode($_SERVER['QUERY_STRING'])) as $url_e){
	if(!preg_match('/offset/', $url_e)){
		$args[] = $url_e;
	}
	$arg_str = implode('&', $args);
}
//echo $arg_str;

$connect = '';
if($req_filter != '' && $proc_filter != ''){
	$connect = ' AND ';
}
$req_filter != '' ? $rf=sql_filters('req_type', $req_filter) : $rf=null;
$proc_filter != '' ? $pf=sql_filters('processing', $proc_filter) : $pf=null;
$case = $cond.$rf.$connect.$pf;

empty($req_filter) ? $req_filter = array() : '';
empty($proc_filter) ? $proc_filter = array() : '';

$navigation = build_navigation(array('', 'Library Reserves'));
print_header('Physical reserve requests', 'Physical reserve requests', $navigation);
if(has_capability('block/moodlelibreserve:allrequests', $context)){
	
	echo '<form id="filters" action="'.$_SERVER['PHP_SELF'].'" method="GET">';
	echo '<div style="margin-bottom:10px;"><div style="width:200px;float:left;padding:10px;border-right:1px solid black;">';
	echo '<input type="checkbox" ';
	echo in_array(1, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="1">Existing - Print</input><br />';

	echo '<input type="checkbox" ';
	echo in_array(2, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="2">To be ordered - Print</input><br />';
	
	echo '<input type="checkbox" ';
	echo in_array(3, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="3">Personal copy - Print</input><br />';

	echo '<input type="checkbox" ';
	echo in_array(4, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="4">Existing - Video</input><br />';

	echo '<input type="checkbox" ';
	echo in_array(5, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="5">To be ordered - Video</input><br />';
	
	echo '<input type="checkbox" ';
	echo in_array(6, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="6">Personal copy - Video</input><br />';

	echo '<input type="checkbox" ';
	echo in_array(7, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="7">Existing - Audio</input><br />';

	echo '<input type="checkbox" ';
	echo in_array(8, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="8">To be ordered - Audio</input><br />';
	
	echo '<input type="checkbox" ';
	echo in_array(9, $req_filter) ? 'checked ' : '';
	echo 'name="req_filters[]" value="9">Personal copy - Audio</input><br />';



	echo '</div>';
	echo '<div style="width:200px;float:left;padding:10px;"><input type="checkbox" ';
	echo in_array(0, $proc_filter) ? 'checked ' : '';
	echo 'name="proc_filters[]" value="0">Not being processed</input><br />';

	echo '<input type="checkbox" ';
	echo in_array(1, $proc_filter) ? 'checked ' : '';
	echo 'name="proc_filters[]" value="1">Being processed</input><br />';
	echo '<input type="checkbox" ';
	echo in_array(2, $proc_filter) ? 'checked ' : '';
	echo 'name="proc_filters[]" value="2">Processed</input></div><br style="height:0px;clear:both;">';
	echo '<input type="submit" value="filter" /></div>';
	$res_count = get_record_sql("SELECT count(*) AS count FROM {$CFG->prefix}libres $case");
	$rs = get_records_sql("SELECT * FROM {$CFG->prefix}libres {$case} LIMIT {$offset},{$rec_per_pg}");
	$curr_page = ceil(($offset/$rec_per_pg)+1);
//	echo "Pages: ";
	$last = end(range(1, ceil(($res_count->count+1)/$rec_per_pg)));
//	foreach(range(1, ceil(($res_count->count+1)/$rec_per_pg)) as $res_number){
//		$link_offset = ($res_number-1)*$rec_per_pg;
//		if($res_number == $curr_page){
//			$num_style = 'style="background-color:#ccc;" ';
//		}else{
//			$num_style = '';
//		}
//		
//		echo '<a '.$num_style.'href="'.$CFG->wwwroot.'/blocks/moodlelibreserve/librarian.php?offset='.$link_offset.'&'.$arg_str.'" onmousedown="javascript:document.forms[\'filters\'].submit()">'.$res_number.'</a> ';
//	}
	echo '</form>';
	echo libres_pagination($res_count->count, $rec_per_pg, 'librarian.php', $arg_str, $curr_page);
	echo '<div id="libres_container" style="padding:5px;">';
	foreach($rs as $op){
		$req_type = get_record_sql("SELECT description, id FROM {$CFG->prefix}request_types WHERE id=$op->req_type");
		echo '<div id="reserve_box_'.$op->id.'" class="reserve_box">';
		
		if($req_type->id == '2' || $req_type->id == '5' || $req_type->id == '8'){
			echo '<div class="order">This item needs to be ordered: <a href="'.$CFG->wwwroot.'/blocks/moodlelibreserve/record.php?id='.$op->id.'">Download datafile</a></div>';
		}
		
		$course = get_record_sql("SELECT fullname, shortname, idnumber FROM {$CFG->prefix}course WHERE id=$op->course");
		$timecreated = date("Y-m-d H:i:s", $op->timecreated);
		$date = new DateTime($timecreated);
		if(in_array($op->req_type, array('2', '5', '8'))){
			$date->modify('+56 days');
		}else{
			$date->modify('+14 days');
		}
		$month = $date->format("m");
		echo "<p style=\"color:red;font-weight:bold;font-size:14px;\">Due date: ".$months[intval($month)]." ".$date->format("d Y")."</p>";
		echo "<p><b>Course:</b> $course->fullname<br /> $course->idnumber</p>";
		$user = get_record_sql("SELECT firstname, lastname, email FROM {$CFG->prefix}user WHERE id=$op->user_id");
		echo "<p><b>Instructor:</b> $user->firstname $user->lastname (<a href=\"mailto:$user->email\">$user->email</a>)</p>";
		echo "<p><i>$req_type->description</i> $req_type->id</p>";
		$req_type = $op->req_type;
		echo "<div style=\"background-color:#ccc;padding:2px;\"><b>$op->title, $op->author";
		if($op->year && $op->year !== ''){
			echo ", $op->year";
		}
		echo "</b></div>";
		if($op->processing == 2){
			echo "<p><b>Call No: </b>".$op->call_num."</p>";
		}else{
			echo "<p><b>$cat_id[$req_type]:</b> ".$op->{$cat_tb[$req_type]}."</p>";
		}
		echo '<form id="status'.$op->id.'">';
		echo '<input type="radio" name="req_status" id="req_status" value="0" ';
		if($op->processing == 0){echo 'checked="true"';}
		echo " />is not being processed.<br />";
		echo '<input type="radio" name="req_status" id="req_status" value="1" ';	
		if($op->processing == 1){echo 'checked="true"';}
		echo '/>is being processed.<br />';

		if($op->req_type != '1' && $op->req_type != '4' && $op->req_type != '7'){
			$need_call = 'onmousedown="enter_callno('.$op->id.');"';
		}else{
			$need_call = '';
		}
		echo '<input '.$need_call.' type="radio" name="req_status" id="req_status" value="2" ';
		if($op->processing == 2){echo 'checked="true"';}
		echo '/>has been processed.<br />';
		echo '<div id="call_num'.$op->id.'" style="display:none;">Newly ordered books or faculty copies, require a library call number.<br />';
		echo '<input type="text" value="'.$op->call_num.'" name="call_num" /></div>';
		echo '</form>';
		
		echo '<p style="text-align:center;"><a onmousedown="set_request_status('.$op->id.', document.getElementById(\'status'.$op->id.'\'), \''.$CFG->wwwroot.'\'); return false;">Save</a></p>';

		echo '<p style="text-align:center"><a onmousedown="delete_request('.$op->id.', \''.$CFG->wwwroot.'\');return false;">Delete this request</a></p>';

		echo libres_comments('', $op->id, 'form');
		echo '</div>';
	}
	echo '<br style="height:0px;clear:both;" />';
	echo '</div>';
}else{
	echo '<div style="font-size:14px;font-weight:bold;color:red;">Sorry, you do not have permission to view this page.</div>';
}
print_footer();
?>
